package com.chenfan.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.process.common.constants.CommonFieldConstants;
import com.chenfan.process.common.utils.QueryWrapperUtils;
import com.chenfan.process.entity.dto.FormQueryDTO;
import com.chenfan.process.entity.po.ProcessForm;
import com.chenfan.process.repository.ProcessFormRepository;
import com.chenfan.process.service.ProcessFormService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author liuming
 * @date 2021/11/24
 */
@Service
public class ProcessFormServiceImpl extends ServiceImpl<ProcessFormRepository, ProcessForm>
    implements ProcessFormService {

    @Override
    public Page<ProcessForm> pageProcessForm(FormQueryDTO form, Integer pageNum, Integer pageSize) {
        Page<ProcessForm> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProcessForm> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(form.getFormName()), ProcessForm.Constants.FORM_NAME,
            form.getFormName());
        queryWrapper.like(!StringUtils.isEmpty(form.getCreateName()), CommonFieldConstants.CREATE_NAME,
            form.getCreateName());
        queryWrapper.eq(Objects.nonNull(form.getStatus()), CommonFieldConstants.STATUS, form.getStatus());
        queryWrapper.orderByDesc(CommonFieldConstants.CREATE_TIME);
        return this.page(page, queryWrapper);
    }

    @Override
    public ProcessForm getByFormName(String formName) {
        QueryWrapper<ProcessForm> queryWrapper = QueryWrapperUtils.newQueryWrapper();
        queryWrapper.eq(ProcessForm.Constants.FORM_NAME, formName);
        return this.getOne(queryWrapper);
    }
}
